package com.googlecode.kipler.syntax.concept;

import com.googlecode.kipler.common.Copyable;
import com.googlecode.kipler.syntax.Negatable;

/**
 * This class represents a singleton named individual set.
 * 
 * @author İnanç Seylan
 */
public class Nominal extends Concept {
	private boolean negated;

	private String name;

	/**
	 * Default constructor
	 */
	public Nominal() {

	}

	/**
	 * Constructor to set the name field
	 * 
	 * @param name
	 *            The concept name to be set
	 */
	public Nominal(String name) {
		setName(name);
	}

	public String getName() {
		return name.toString();
	}

	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @see Negatable#isNegated()
	 */
	public boolean isNegated() {
		return negated;
	}

	/**
	 * @see Negatable#toggleNegated()
	 */
	public Concept toggleNegated() {
		negated = !negated;
		return this;
	}

	/**
	 * @see Copyable#copy()
	 */
	public Nominal copy() {
		Nominal n = new Nominal();
		n.name = name;
		n.negated = negated;
		n.setLeftBiased(isLeftBiased());
		return n;
	}

	/**
	 * @return true if the other class is a nominal with the same named
	 *         individual and negation status.
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj != null && (obj.getClass().equals(this.getClass()))) {
			Nominal other = (Nominal) obj;
			return (this.isNegated() == other.isNegated()
					&& isLeftBiased() == other.isLeftBiased() && this.name
					.equals(other.name));
		}
		return false;
	}

	/**
	 * @see Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return 17 * 37 + new Boolean(negated).hashCode()
				+ new Boolean(isLeftBiased()).hashCode() + name.hashCode();
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		String name = "{" + this.name + "}";
		return (isNegated() ? "~" + name : name);
	}

	/**
	 * Calls {@link ConceptVisitor#visitNominal(Nominal)} on itself.
	 */
	public void accept(ConceptVisitor v) {
		v.visitNominal(this);
	}

}
